<html>
<head><meta charset="utf-8"><title>Better support of Box&lt;MaybeUnit&lt;T&gt;&gt; pattern. · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html">Better support of Box&lt;MaybeUnit&lt;T&gt;&gt; pattern.</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="172777615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172777615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172777615">(Aug 08 2019 at 15:58)</a>:</h4>
<p>I think actually we're very close to providing a useful "placement new" mechanism, with Box&lt;MaybeUnit&lt;T&gt;&gt; pattern... Most of the things can be implemented at the library level, only with a few "primitives" needed.</p>



<a name="172777792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172777792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172777792">(Aug 08 2019 at 15:59)</a>:</h4>
<p>It's especially useful when T is a very big type. Instead of moving from stack to heap when constructing Box&lt;T&gt;, people can just build a Box&lt;MaybeUnit&lt;T&gt;&gt; somehow, initialize it somehow, and assume_init and call it done~</p>



<a name="172778325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172778325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172778325">(Aug 08 2019 at 16:05)</a>:</h4>
<p><span class="user-mention" data-user-id="116458">@Charles Lew</span> <a href="https://github.com/rust-lang/rust/pull/62451" target="_blank" title="https://github.com/rust-lang/rust/pull/62451">https://github.com/rust-lang/rust/pull/62451</a></p>



<a name="172778393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172778393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172778393">(Aug 08 2019 at 16:05)</a>:</h4>
<p><span class="user-mention" data-user-id="126931">@centril</span>  Yay~~</p>



<a name="172778455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172778455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172778455">(Aug 08 2019 at 16:06)</a>:</h4>
<p>good to know, thx~</p>



<a name="172778490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172778490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172778490">(Aug 08 2019 at 16:06)</a>:</h4>
<p>np</p>



<a name="172784397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172784397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172784397">(Aug 08 2019 at 17:23)</a>:</h4>
<p><span class="user-mention" data-user-id="116458">@Charles Lew</span> do you have a library implementation ?</p>



<a name="172784450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172784450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172784450">(Aug 08 2019 at 17:23)</a>:</h4>
<p>I can imagine particular implementations for different use cases, but I wouldn't know how to write one that works even for the simple case of constructing a single value</p>



<a name="172784705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172784705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172784705">(Aug 08 2019 at 17:26)</a>:</h4>
<p>E.g. if I wanted to initialize a <code>Box&lt;[u32; N]&gt;</code> where <code>N</code> is big,  I'd probably have some API like:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">array_box</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">F</span>: <span class="nb">Fn</span><span class="p">(</span><span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">(</span><span class="n">f</span>: <span class="nc">F</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Box</span><span class="o">&lt;</span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">b</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="n">MaybeUninit</span><span class="o">&lt;</span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="n">MaybeUninit</span>::<span class="n">uninit</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="p">..</span><span class="n">N</span><span class="w"> </span><span class="p">{</span><span class="w">  </span><span class="n">ptr</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="n">write</span><span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">i</span><span class="p">));</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">b</span><span class="p">.</span><span class="n">assume_init</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="172784800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172784800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172784800">(Aug 08 2019 at 17:27)</a>:</h4>
<p>But that breaks if <code>T</code> is too big, like, another big array , so that you end up with <code>Box&lt;[[T; M]; N&gt;</code> where both M and N are big</p>



<a name="172784877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172784877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172784877">(Aug 08 2019 at 17:28)</a>:</h4>
<p>And if instead of a <code>Box</code> I want a <code>Vec&lt;[T; N]&gt;</code> I probably need something completely different</p>



<a name="172793356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172793356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172793356">(Aug 08 2019 at 18:55)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span>  i dont have a library implementation. But i guess at least the non-generics cases are satisfied.</p>



<a name="172793462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better%20support%20of%20Box%3CMaybeUnit%3CT%3E%3E%20pattern./near/172793462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Better.20support.20of.20Box.3CMaybeUnit.3CT.3E.3E.20pattern.2E.html#172793462">(Aug 08 2019 at 18:57)</a>:</h4>
<p>i guess being generic over the "shape" of data is tricky.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>